<?php

namespace app\index\service;

use app\index\model\ResourceRecommend as ResourceRecommendModel;
use think\facade\Cache;
use think\facade\Log;

class ResourceRecommendService
{
    /**
     * 处理社群推荐 5分钟更新一条 上限10条  type值：1=>检索栏推荐 2=>固定栏推荐
     * @param $type
     * @param $limit
     * @param int $time
     */
    public static function recommendedList($type, $limit, $time = 320)
    {
        // 删除过期数据
        ResourceRecommendModel::delResourceRecommend([['end_time', '<', time()]]);

        $recommend = ResourceRecommendModel::columnResourceRecommend($type, $limit);

        $where = ['type' => $type];
        if (count($recommend) >= $limit) {
            $where['rid'] = $recommend[0];
        }
        ResourceRecommendModel::updateResourceRecommend(['show_time' => time(), 'status' => 1], $where);

//        var_dump($recommend);

        // 每隔320秒更新一次数据
        Cache::set('resource_recommended_' . $type, json_encode($recommend), $time);

        Log::write('本次推荐的最新社群数据' . json_encode($recommend), 'error');

    }
}